<?php
declare (strict_types=1);

namespace app\user\model\user\admin;

use app\common\model\BaseModel;

/**
 * 定义用户收入模型
 */
class CreditFlow extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'user_credit_flow';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'      => 'integer',
        'user_id' => 'integer',
        'type'    => 'integer',
        'num'     => 'float',
        'credit'  => 'float'
    ];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    public function getAlterTypeAttr($value, $data)
    {
        return in_array($data['type'], [1, 3, 4, 8, 9]) ? 1 : 0;
    }

    public function getTypeTextAttr($value, $data)
    {
        $text = [
            1  => '充值',
            2  => '冻结',
            3  => '解冻',
            4  => '操作',
            5  => '兑换收入',
            6  => '兑换商品',
            7  => '积分抵扣',
            8  => '订单退还',
            9  => '系统增加',
            10 => '系统扣除'
        ];
        return $data['type_text'] = $text[$data['type']];
    }

    /**
     * 积分获得类型
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-08-23
     *
     * @param string $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function getRelTypeTextAttr($value = '', $data)
    {
        $arr = $this->relTypeArr();

        return $arr[$data['rel_type']] ?? '系统操作';
    }

    public function relTypeArr()
    {
        return [
            'register'       => '注册',
            'sign_in'        => '签到',
            'recharge'       => '充值',
            'course'         => '购买课程',
            'share_course'   => '分享课程',
            'comment_course' => '评论课程',
            'vip'            => '充值会员'
        ];
    }
}
